<template>
  <Container>
    <Tabs v-model="tab">
      <TabPane label="巡店设置" name="tab1">
        <homeTable
          :list="list"
          :hasPage="false"
          @getData="getData"/>
      </TabPane>
    </Tabs>
    <PartolEditItem ref="edit" @complete="getData"></PartolEditItem>
    <div class="container-footer bg-white" slot="footer">
      <div class="duration-set">
        <div class="bold">
          拜访最短时长（分钟）
          <InputNumber style="width:120px" placeholder="请输入最短时长" :precision="1" v-model="minute" :min="0"/>
        </div>
        <Button type="primary" size="large" class="ml" @click="onSave" :disabled="disabled">保存</Button>
      </div>
    </div>
  </Container>
</template>
<script>
import homeTable from '~/homeTable'
import PatrolListData from './list/PatrolListData'
import PartolEditItem from './list/PartolEditItem'
export default {
  components: {
    homeTable,
    PartolEditItem
  },
  data() {
    return {
      tab: 'tab1',
      list: PatrolListData,
      minute: null,
      disabled: false
    }
  },
  mounted() {
    this.getMinute()
    this.getData()
    this.$bus.$on('onEdit', row => this.onEdit(row))
    // this.$refs.edit.open()
  },
  methods: {
    getData() {
      this.$ajaxGet('/setting/salesman_inspection/list').then(res => {
        res.map(item => {
          let rolesArray = []
          if (item.roles.length) {
            item.roles.forEach(child => {
              rolesArray.push(child.desc)
            })
          }
          item.roleStrs = rolesArray.join(',')
        })
        this.list.data = res
        this.list.status = 'pass'
      }).catch(() => {
        this.list.status = 'error'
      })
    },
    getMinute() {
      this.$ajaxGet('/salesman/config/inspection/duration/search').then(data => {
        this.minute = Number(data)
      })
    },
    onEdit(row) {
      this.$refs.edit.open(row)
    },
    onSave() {
      if (this.disabled) return
      this.disabled = true
      let params = {
        minute: this.minute
      }
      this.$ajaxPost('/salesman/config/inspection/duration/set', params).then(() => {
        this.$Notice.success({
          title: '修改成功'
        })
        this.disabled = false
      }).catch(() => {
        this.disabled = false
      })
    }
  },
  destroyed() {
    this.$bus.$off()
  }
}
</script>